from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


class User(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __repr__(self):
        return f"<User {self.username}>"


class Post(db.Model):
    __tablename__ = "posts"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)

    user = db.relationship("User", backref=db.backref("posts", lazy=True))

    def __repr__(self):
        return f"<Post {self.title}>"


class Comment(db.Model):
    __tablename__ = "comments"
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)

    post = db.relationship("Post", backref=db.backref("comments", lazy=True))
    user = db.relationship("User", backref=db.backref("comments", lazy=True))

    def __repr__(self):
        return f"<Comment {self.content}>"


class Category(db.Model):
    __tablename__ = "categories"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)

    def __repr__(self):
        return f"<Category {self.name}>"


class PostCategory(db.Model):
    __tablename__ = "post_categories"
    post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), primary_key=True)
    category_id = db.Column(
        db.Integer, db.ForeignKey("categories.id"), primary_key=True
    )

    post = db.relationship("Post", backref=db.backref("post_categories", lazy=True))
    category = db.relationship(
        "Category", backref=db.backref("post_categories", lazy=True)
    )

    def __repr__(self):
        return f"<PostCategory {self.post_id} - {self.category_id}>"


class Tag(db.Model):
    __tablename__ = "tags"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)

    def __repr__(self):
        return f"<Tag {self.name}>"


class PostTag(db.Model):
    __tablename__ = "post_tags"
    post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), primary_key=True)
    tag_id = db.Column(db.Integer, db.ForeignKey("tags.id"), primary_key=True)

    post = db.relationship("Post", backref=db.backref("post_tags", lazy=True))
    tag = db.relationship("Tag", backref=db.backref("post_tags", lazy=True))

    def __repr__(self):
        return f"<PostTag {self.post_id} - {self.tag_id}>"


class Like(db.Model):
    __tablename__ = "likes"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)
    post_id = db.Column(db.Integer, db.ForeignKey("posts.id"), nullable=False)

    user = db.relationship("User", backref=db.backref("likes", lazy=True))
    post = db.relationship("Post", backref=db.backref("likes", lazy=True))

    def __repr__(self):
        return f"<Like {self.user_id} - {self.post_id}>"
